package ycl.common.config;

import cn.hutool.core.util.StrUtil;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import ycl.common.properties.RedisProperties;

import javax.annotation.Resource;
import java.time.Duration;

/**
 * redis配置
 *
 * @author ycl
 * @date 2022/6/17 0017 16:11:36
 */
@Configuration
public class RedisConfig {

	@Resource
	private RedisProperties redisProperties;


	@Primary
	@Bean(name = "stringRedisTemplate0")
	public StringRedisTemplate stringRedisTemplate0() {
		return stringRedisTemplate(0);
	}


	@Bean(name = "stringRedisTemplate1")
	public StringRedisTemplate stringRedisTemplate1() {
		return stringRedisTemplate(redisProperties.getDb1());
	}

	@Bean(name = "stringRedisTemplate2")
	public StringRedisTemplate stringRedisTemplate2() {
		return stringRedisTemplate(redisProperties.getDb2());
	}

	@Bean(name = "stringRedisTemplate3")
	public StringRedisTemplate stringRedisTemplate3() {
		return stringRedisTemplate(redisProperties.getDb3());
	}

	@Bean(name = "stringRedisTemplate4")
	public StringRedisTemplate stringRedisTemplate4() {
		return stringRedisTemplate(redisProperties.getDb4());
	}

	@Bean(name = "stringRedisTemplate5")
	public StringRedisTemplate stringRedisTemplate5() {
		return stringRedisTemplate(redisProperties.getDb5());
	}

	@Bean(name = "stringRedisTemplate6")
	public StringRedisTemplate stringRedisTemplate6() {
		return stringRedisTemplate(redisProperties.getDb6());
	}

	@Bean(name = "stringRedisTemplate7")
	public StringRedisTemplate stringRedisTemplate7() {
		return stringRedisTemplate(redisProperties.getDb7());
	}

	@Bean(name = "stringRedisTemplate8")
	public StringRedisTemplate stringRedisTemplate8() {
		return stringRedisTemplate(redisProperties.getDb8());
	}

	@Bean(name = "stringRedisTemplate9")
	public StringRedisTemplate stringRedisTemplate9() {
		return stringRedisTemplate(redisProperties.getDb9());
	}

	@Bean(name = "stringRedisTemplate10")
	public StringRedisTemplate stringRedisTemplate10() {
		return stringRedisTemplate(redisProperties.getDb10());
	}

	@Bean(name = "stringRedisTemplate11")
	public StringRedisTemplate stringRedisTemplate11() {
		return stringRedisTemplate(redisProperties.getDb11());
	}

	@Bean(name = "stringRedisTemplate12")
	public StringRedisTemplate stringRedisTemplate12() {
		return stringRedisTemplate(redisProperties.getDb12());
	}

	@Bean(name = "stringRedisTemplate13")
	public StringRedisTemplate stringRedisTemplate13() {
		return stringRedisTemplate(redisProperties.getDb13());
	}

	@Bean(name = "stringRedisTemplate14")
	public StringRedisTemplate stringRedisTemplate14() {
		return stringRedisTemplate(redisProperties.getDb14());
	}

	@Bean(name = "stringRedisTemplate15")
	public StringRedisTemplate stringRedisTemplate15() {
		return stringRedisTemplate(redisProperties.getDb15());
	}

	/**
	 * 重构string redis template
	 *
	 * @param db
	 * @return
	 */
	private StringRedisTemplate stringRedisTemplate(Integer db) {
		if (db == null)
			return null;
		RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
		configuration.setHostName(redisProperties.getHost());//设计ip
		configuration.setPort(redisProperties.getPost());//设置端口
		String password = redisProperties.getPassword();
		if (StrUtil.isNotBlank(password))
			configuration.setPassword(password);
		LettuceClientConfiguration client = LettuceClientConfiguration.builder()
				.commandTimeout(Duration.ofSeconds(redisProperties.getTimeout()))
				.build();
		//通过工厂类获取配置对象
		LettuceConnectionFactory factory = new LettuceConnectionFactory(configuration, client);
		factory.setDatabase(db);
		factory.afterPropertiesSet();
		return new StringRedisTemplate(factory);
	}
}
